/*
* ===================================================
*
*       Filename:  foodworker.cc
*
*
*        Created:  12/12/2011 03:45:16 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  durand_y, <durand.yoan@gmail.com>
*
* ===================================================
*/

#include "foodworker.hh"
#include "map.hh"

FoodWorker::FoodWorker (int x, int y, bool is_queen)
	: Worker (x, y, is_queen)
{
}

FoodWorker::~FoodWorker ()
{
}

void
FoodWorker::print ()
{
	if (resource_ == true)
		std::cout << "F" << team_;
	else
		std::cout << "f" << team_;
}

void
FoodWorker::drop_phero ()
{
	std::cout << "droped" << std::endl;
}

void
FoodWorker::move (int x, int y)
{
	int cell = 0;
	Map& m = Map::GetInstance ();
	int cpt = 0;

	cell = my_rand () % 8;
	while ((cpt < 10) && (new_cell (x, y, cell) == false))
	{
		if (new_cell (x, y, cell) == true)
		{
			m.get_map ()[pos_y_][pos_x_]->set_piece (0);
			m.get_map()[x][y]->set_piece (this);
			return ;
		}
		else
		{
			cell = my_rand () % 8;
			cpt++;
		}
	}
}


void
FoodWorker::action ()
{
	int max = 0;
	int tmp = 0;
	int newx = 0;
	int newy = 0;

	Map& m = Map::GetInstance ();

	for (int i =pos_y_ - 1; i < pos_y_ + 2; i++)
	{
		for (int j = pos_x_ - 1; j < pos_x_ + 2; j++)
		{
			if (m.get_map ()[i][j]->get_piece () == 0)
			{
				if (resource_ == false)
					tmp = m.get_map ()[i][j]->commute_interest ("food", team_);
				else
					tmp = m.get_map ()[i][j]->commute_interest ("queen", team_);
			}
			else
				 m.get_map ()[i][j]->set_interest (0);
			if (tmp > max)
			{
				max = tmp;
				newx = j;
				newy = i;
			}
		}
	}

	move (newx, newy);

}

bool
FoodWorker::new_cell (int x, int y, int cell)
{
	int newx = 0;
	int newy = 0;

	newx = pos_x_;
	newy = pos_y_;

		if (cell == 0)
			newy -= 1;
		if (cell == 1)
		{
			newy -= 1;
			newx += 1;
		}
		if (cell == 2)
			newx += 1;
		if (cell == 3)
		{
			newy += 1;
			newx += 1;
		}
		if (cell == 4)
			newy += 1;
		if (cell == 5)
		{
			newy += 1;
			newx -= 1;
		}
		if (cell == 6)
			newx -= 1;
		if (cell == 7)
		{
			newy -= 1;
			newx -= 1;
		}

		if ((newx == x) && (newy == y))
			return (true);
		else
			return (false);
}
